package lib.gui;

import lib.BitmapButtonField;
import lib.TwoColumnField;
import lib.TwoColumnFieldManager;
import lib.Util;
import lib.classes.Profile;
import lib.classes.VerticalFieldManagerFieldSet;
import net.rim.device.api.system.Display;
import net.rim.device.api.system.EncodedImage;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.component.AutoTextEditField;
import net.rim.device.api.ui.component.DateField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ObjectChoiceField;
import net.rim.device.api.ui.component.TreeField;
import net.rim.device.api.ui.component.TreeFieldCallback;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.extension.container.ZoomScreen;

public class ProfileScreen extends Page implements TreeFieldCallback
{
	BitmapButtonField profilePic;
	AutoTextEditField screenName;
	AutoTextEditField status;
	AutoTextEditField aboutYou;
	ObjectChoiceField gender;
	DateField dob;
	AutoTextEditField city;
	AutoTextEditField province;
	AutoTextEditField country;
	VerticalFieldManager p;
	private Profile profile;
	TreeField aboutmePane;
	private boolean isReadOnly;
	
	
	public ProfileScreen(Profile _p) 
	{
		super(_p.getScreenname());
		this.profile = _p;
		LabelField _status = new LabelField(profile.getStatus());
		_status.setPadding(5, 5,5,5);
		_status.setFont(_status.getFont().derive(Font.ITALIC, 20));
		add(_status);
		
		//VerticalFieldManagerFieldSet row1 = new VerticalFieldManagerFieldSet();
		EncodedImage _ei = Util.scaleImageToWidth(profile.getImage(), 80);
        _ei = Util.scaleImageToHeight(_ei, 80);
        p = new VerticalFieldManager(USE_ALL_WIDTH);
        profilePic = new BitmapButtonField(_ei.getBitmap());
        profilePic.setChangeListener(this);
        p.add(profilePic);
        
        VerticalFieldManager col2 = new VerticalFieldManager();
        LabelField name = new LabelField(profile.getScreenname()){
			public void paint(Graphics g)
		    {
		        g.setColor(Color.WHITE);		        
		        super.paint(g);
		    } 
		};
        name.setPadding(5,5,5,5);
        name.setFont(name.getFont().derive(Font.BOLD, 20));
        col2.add(name);
        
        LabelField _gender = new LabelField(profile.getGender().toUpperCase()){
			public void paint(Graphics g)
		    {
		        g.setColor(Color.DARKGRAY);		        
		        super.paint(g);
		    } 
		};
        _gender.setFont(name.getFont().derive(Font.BOLD, 16));
        
        LabelField _age = new LabelField(profile.getAge() + "" , DrawStyle.RIGHT){
			public void paint(Graphics g)
		    {
		        g.setColor(Color.DARKGRAY);		        
		        super.paint(g);
		    } 
		};
        _age.setFont(name.getFont().derive(Font.BOLD, 16));
        
        HorizontalFieldManager _tmp = new HorizontalFieldManager()
        {
        	protected void sublayout(int width, int height) 
			{	            
	            int _w = Display.getWidth() - 140;
	            this.setExtent(_w, height);
	            super.layout(width, height);
	        }
        };
        _tmp.add(_age);
        _tmp.add(_gender);
        _tmp.setPadding(4,4,4,4);
        col2.add(_tmp);
        
        TwoColumnField row1 = new TwoColumnField(p, col2);
        TwoColumnFieldManager r1 = new TwoColumnFieldManager(140);
        row1.setPadding(5, 5, 5, 5);
        r1.add(row1);
        add(r1);
        
        VerticalFieldManagerFieldSet _row = new VerticalFieldManagerFieldSet();
        LabelField location = new LabelField(profile.getLocation().toString());
        location.setPadding(5,5,5,5);
        _row.add(location);
        add(_row);
        aboutmePane = new TreeField(this, USE_ALL_WIDTH | Field.FOCUSABLE);
        aboutmePane.setDefaultExpanded(false);
        aboutmePane.addChildNode(0, new LabelField("About Me"));
        aboutmePane.addChildNode(0, profile.getAboutme());
        aboutmePane.setPadding(3,0,3,5);
        /*LabelField topic = new LabelField("About Me")
        {
	        public void paint(Graphics g)
		    {
		        g.setColor(Color.WHITE);		        
		        super.paint(g);
		    } 
		};
        topic.setPadding(3,0,3,5);
        add(topic);
        VerticalFieldManagerFieldSet row2 = new VerticalFieldManagerFieldSet();
        row2.add(new LabelField(profile.getAboutme()));
        add(row2);*/
        add(aboutmePane);
        setReadOnly(true);
        
	}
	
	public ProfileScreen(Profile profile, boolean readOnly) 
	{
		this(profile);
		setReadOnly(readOnly);
	}
	
	public void fieldChanged(Field field, int context) 
	{
		if(field == profilePic)
		{
			pushScreen(new ZoomScreen(profile.getImage()));
		}
	}
	public void drawTreeItem(TreeField treeField, Graphics graphics, int node,
			int y, int width, int indent) 
	{
		if( treeField == aboutmePane ) 
        {        
            Object cookie = aboutmePane.getCookie(node);           
                    
            if(cookie instanceof String) 
            {
                String text = (String)cookie;
                graphics.drawText(text, indent, y, Graphics.ELLIPSIS, width);
            }
            if(cookie instanceof LabelField)
            {
            	String text = ((LabelField)cookie).getText();
                graphics.drawText(text, indent, y, Graphics.ELLIPSIS, width);
            }
            /*
            if(cookie instanceof Category) 
            {
                Category category = (Category)cookie;
                String text = category.getName();
                graphics.drawText(text, indent, y, Graphics.ELLIPSIS, width);
            }*/
        }        
		
	}

	public boolean isReadOnly() {
		return isReadOnly;
	}

	public void setReadOnly(boolean isReadOnly) {
		this.isReadOnly = isReadOnly;
	}

}
